package com.mallcai.bigdata.ladon.dpl.mapper.system.provider;

import com.mallcai.bigdata.ladon.dpl.entity.system.Resource;
import com.mallcai.bigdata.ladon.dpl.mapper.BaseSqlProvider;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author gst
 * @date 2019-12-30 20:36
 */
public class ResourceSqlProvider extends BaseSqlProvider<Resource> {

    public static final String TB_RES = "sys_resource";

    /**
     * 获取表名
     *
     * @return String
     */
    @Override
    public String getTableName() {
        return TB_RES;
    }

    /**
     * 获取插入数据的字段列表
     *
     * @return Map
     */
    @Override
    public Map<String, String> getInsertColumns() {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("parent_id", "#{parentId}");
        map.put("level", "#{level}");
        map.put("name", "#{name}");
        map.put("path", "#{path}");
        map.put("type", "#{type}");
        map.put("auth_code", "#{authCode}");
        map.put("icon", "#{icon}");
        map.put("sort_id", "#{sortId}");
        map.put("comment", "#{comment}");
        return map;
    }

    /**
     * 获取更新操作的字段列表
     *
     * @return Map
     */
    @Override
    public Map<String, String> getUpdateColumns() {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("parentId", "parent_id");
        map.put("level", "level");
        map.put("name", "name");
        map.put("path", "path");
        map.put("type", "type");
        map.put("authCode", "auth_code");
        map.put("icon", "icon");
        map.put("sortId", "sort_id");
        map.put("comment", "comment");
        return map;
    }

    /**
     * 根据参数查询
     */
    public String getByParams(Map<String, Object> map) {
        Map<String, Object> params = getParams(map);

        StringBuilder sb = new StringBuilder();
        sb.append("archive = 0");
        if (params.containsKey("name") && StringUtils.isNotBlank(params.get("name").toString())) {
            sb.append(" and name like concat('%', #{params.name}, '%')");
        }
        if (params.containsKey("authCode") && StringUtils.isNotBlank(params.get("authCode").toString())) {
            sb.append(" and auth_code like concat('%', #{params.authCode}, '%')");
        }
        if (params.containsKey("code") && StringUtils.isNotBlank(params.get("code").toString())) {
            sb.append(" and code like concat('%', #{params.code}, '%')");
        }
        if (params.containsKey("parentId")) {
            sb.append(" and parent_id = #{params.parentId}");
        }
        return getCommonSql(sb.toString());
    }

    public String getChildren(@Param("parentIds") List<Long> parentIds) {
        StringBuilder sb = new StringBuilder();
        if (parentIds != null && parentIds.size() > 0) {
            sb.append(" parent_id IN (");
            for (int i = 0; i < parentIds.size(); i++) {
                if (i > 0) {
                    sb.append(", ");
                }
                sb.append(parentIds.get(i));
            }
            sb.append(")");
        } else {
            sb.append(" archive = 0 AND 1 = 0");
        }

        return getCommonSql(sb.toString());
    }

    public String getByIds(@Param("ids") List<Long> ids) {
        StringBuilder sb = new StringBuilder();
        if (ids != null && ids.size() > 0) {
            sb.append(" id IN (");
            for (int i = 0; i < ids.size(); i++) {
                if (i > 0) {
                    sb.append(", ");
                }
                sb.append(ids.get(i));
            }
            sb.append(")");
        } else {
            sb.append(" archive = 0 AND 1 = 0");
        }

        return getCommonSql(sb.toString());
    }

    private String getCommonSql(String whereSql) {
        return new SQL() {
            {
                SELECT("*");
                FROM(getTableName());
                WHERE(whereSql);
                ORDER_BY(" parent_id, sort_id ");
            }
        }.toString();
    }

    public String getAuthCodeByUserId(@Param("userId") Long userId) {
        StringBuilder sb = new StringBuilder("auth_code IS NOT NULL ");
        sb.append(" AND archive = 0 ")
                .append(" AND id IN (")
                .append("   SELECT DISTINCT res_id FROM ")
                .append(RoleRlResourceSqlProvider.TB_ROLE_RL_RESOURCE)
                .append("   WHERE archive = 0 AND role_id IN ( ")
                .append("         SELECT role_id FROM ")
                .append(UserRlRoleSqlProvider.TB_USER_RL_ROLE)
                .append("            WHERE archive = 0 AND user_id = #{userId}")
                .append("     )")
                .append(" )");
        return new SQL() {
            {
                SELECT("id, auth_code");
                FROM(getTableName());
                WHERE(sb.toString());
            }
        }.toString();
    }
}
